package com.xbongbong.saas.script.help;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.saas.domain.entity.CustomerUserEntity;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import com.xbongbong.saas.enums.subform.CustomerTeamEnum;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;

/**
 *
 * @author 魏荣杰
 * @date 2020/8/18 17:22
 * @since
 * @version
 */
@Deprecated
public class TransferSubFormHelper {

    public static PaasFormDataEntity transferTeamEntityToPaasFormDataEntity(UserTeamEntity userTeamEntity, Class teamEnumClass) {
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        paasFormDataEntity.setAddTime(userTeamEntity.getAddTime());
        paasFormDataEntity.setUpdateTime(userTeamEntity.getUpdateTime());
        paasFormDataEntity.setCorpid(userTeamEntity.getCorpid());
        paasFormDataEntity.setDel(userTeamEntity.getDel());
        paasFormDataEntity.setId(userTeamEntity.getId());
        JSONObject data = new JSONObject();
        
        Class<? extends UserTeamEntity> teamClass = userTeamEntity.getClass();

        Method getDbAttrMethod = null;
        Method getDataAttrMethod = null;
        Method getFieldTypeMethod = null;
        try {
            getDbAttrMethod = teamEnumClass.getMethod("getDbAttr");
            getDataAttrMethod = teamEnumClass.getMethod("getDataAttr");
            getFieldTypeMethod = teamEnumClass.getMethod("getFieldType");
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        Enum[] enumConstants = (Enum[]) teamEnumClass.getEnumConstants();
        assert getDbAttrMethod != null;
        assert getDataAttrMethod != null;
        assert getFieldTypeMethod != null;
        for (Enum enumConstant : enumConstants) {
            String dbAttr = null;
            String dataAttr = null;
            Integer fieldType = null;
            try {
                dbAttr = (String) getDbAttrMethod.invoke(enumConstant);
                dataAttr = (String) getDataAttrMethod.invoke(enumConstant);
                fieldType = (Integer) getFieldTypeMethod.invoke(enumConstant);

                Method method = teamClass.getMethod("get" + dbAttr.substring(0, 1).toUpperCase() + dbAttr.substring(1));
                Object value = method.invoke(userTeamEntity);
                if (Objects.nonNull(value)) {
                    FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getFieldTypeEnum(fieldType);
                    if (Objects.nonNull(fieldTypeEnum)) {
                        switch (fieldTypeEnum) {
                            case DATAID:
                                break;
                            case TEXT:
                                data.put(dataAttr, value);
                                break;
                            case NUM:
                                data.put(dataAttr, Long.valueOf(value.toString()));
                                break;
                            case COMBO:
                                break;
                            case COMBO_NUM:
                                break;
                            case DATETIME:
                                data.put(dataAttr, Long.valueOf(value.toString()));
                                break;
                            case IMAGE:
                                break;
                            case TEXTAREA:
                                break;
                            case UPLOAD:
                                break;
                            case CHECKBOX_GROUP:
                                break;
                            case STAR:
                                break;
                            case ADDRESS:
                                break;
                            case VIDEO:
                                break;
                            case SWITCH:
                                break;
                            case SELECT_DEPARTMENT:
                                break;
                            case FLOW_STATUS:
                                break;
                            case COUNT:
                                break;
                            case BIRTHDAY:
                                break;
                            case RADIO_GROUP:
                                break;
                            case COMBO_CHECK:
                                break;
                            case SEPARATOR:
                                break;
                            case LOCATION:
                                break;
                            case MEMO:
                                break;
                            case HREF:
                                break;
                            case SUB_FORM:
                                break;
                            case LINK_QUERY:
                                break;
                            case LINK_DATA:
                                break;
                            case USER:
                                break;
                            case USER_GROUP:
                                break;
                            case DEPT:
                                break;
                            case DEPT_GROUP:
                                break;
                            case CREATORID:
                                break;
                            case ADDTIME:
                                break;
                            case UPDATETIME:
                                break;
                            case DEPARTMENTID:
                                break;
                            case OWNERID:
                                break;
                            case COUSERID:
                                break;
                            case SERIALNO:
                                break;
                            case WAREHOUSE_ID:
                                break;
                            case WARINING_TYPE:
                                break;
                            case GUARANTEE_STATUS:
                                break;
                            case CATEGORY_ID:
                                break;
                            case TEAM_USER:
                                break;
                            case TEAM_USER_TYPE:
                                break;
                            case ENDTIME:
                                break;
                            case CLOSETIME:
                                break;
                            case COPY_USER:
                                break;
                            case SURCHARGE:
                                break;
                            case LINK_BUSINESS_SINGLE:
                                break;
                            case LINK_BUSINESS_MULTI:
                                break;
                            case CONTACT_NUMBER:
                                break;
                            case SELECT_PRODUCT:
                                break;
                            case CHECK_COMBO:
                                break;
                            case COMBO_LINK_BUSINESS:
                                break;
                            case SWITCH_TEXT:
                                break;
                            case SPECIFICATION:
                                break;
                            case PRODUCT_STOCK:
                                break;
                            case TEXT_CHECK:
                                break;
                            case RICH_TEXT_EDITOR:
                                break;
                            case LINK_BUSINESS_COMBO:
                                break;
                            case LINK_FORM:
                                break;
                            case SINGLE_SUB_FORM:
                                break;
                            case WORK_ORDER_TIME_OUT:
                                break;
                            case WORK_ORDER_ROB:
                                break;
                            case WORK_ORDER_DATA_TIME_OUT:
                                break;
                            case WORK_ORDER_DATA_TIME_NOT_OUT:
                                break;
                            case WORK_ORDER_STATUS:
                                break;
                            case WORK_ORDER_STAGE:
                                break;
                            case WORK_ORDER_ALL_OUTBOUND:
                                break;
                            case TEMPLATE:
                                break;
                            case WORK_ORDER_NODE_STATUS:
                                break;
                            case LOG_TYPE:
                                break;
                            case WORK_ORDER_STAGE_NAME:
                                break;
                            case PRE_OWNER_ID:
                                break;
                            case PUBLIC_GROUP_PROVINCE:
                                break;
                            case CASCADER:
                                break;
                            case BATCH:
                                break;
                            case OPTION:
                                break;
                            case FOCUS:
                                break;
                            case REMIND:
                                break;
                            case CALL:
                                break;
                            case FLOW:
                                break;
                            case CONTRACT:
                                break;
                            case PRODUCT:
                                break;
                            case STOCK_SEARCH:
                                break;
                            case WAIT_INSTOCK_CONTRACT:
                                break;
                            case WAIT_OUTSTOCK_PRODUCT:
                                break;
                            case HEAD_RADIO_GROUP:
                                break;
                            case HEAD_CHECKBOX_GROUP:
                                break;
                            case WARNING_SEARCH:
                                break;
                            case RANGE_SCREEN:
                                break;
                            case STOCK_WAREHOUSE_ID:
                                break;
                            case WAREHOUSE_CHECKED:
                                break;
                            case WAREHOUSE_IS_NOTIFY:
                                break;
                            case ALL_WAREHOUSE_ALL_LIMIT:
                                break;
                            case ALL_WAREHOUSE_UPPER_LIMIT:
                                break;
                            case ALL_WAREHOUSE_LOWER_LIMIT:
                                break;
                            case CHECKED_WAREHOUSE_ALL_LIMIT:
                                break;
                            case CHECKED_WAREHOUSE_UPPER_LIMIT:
                                break;
                            case CHECKED_WAREHOUSE_LOWER_LIMIT:
                                break;
                            case BATCH_WAREHOUSE_ID:
                                break;
                            case BATCH_WAREHOUSE_CHECKED:
                                break;
                            case BATCH_PRODUCE_DATE:
                                break;
                            case BUSINESS_ID:
                                break;
                            case REF_ID:
                                break;
                            case STOCK_FLOW_SUB_FORM:
                                break;
                            case STOCK_FLOW_SHEET_DATE:
                                break;
                            case CREATOR_DEPT:
                                break;
                            case SHEET_ALONE:
                                break;
                            case SHEET_UUID:
                                break;
                            case LINK_FORM_BUTTON:
                                break;
                            default:
                                data.put(dataAttr, value);
                        }
                    }
                }

            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
        paasFormDataEntity.setData(data);

        return paasFormDataEntity;
    }

    public static UserTeamEntity transferPaasFormDataEntityToTeamEntity(PaasFormDataEntity paasFormDataEntity, Class teamEnumClass, Class<? extends UserTeamEntity> userTeamClass) {
        UserTeamEntity userTeamEntity = null;
        try {
            userTeamEntity = userTeamClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        userTeamEntity.setAddTime(paasFormDataEntity.getAddTime());
        userTeamEntity.setUpdateTime(paasFormDataEntity.getUpdateTime());
        userTeamEntity.setCorpid(paasFormDataEntity.getCorpid());
        userTeamEntity.setDel(paasFormDataEntity.getDel());
        userTeamEntity.setId(paasFormDataEntity.getId());

        JSONObject data = paasFormDataEntity.getData();

        Method getDbAttrMethod = null;
        Method getDataAttrMethod = null;
        Method getFieldTypeMethod = null;
        try {
            getDbAttrMethod = teamEnumClass.getMethod("getDbAttr");
            getDataAttrMethod = teamEnumClass.getMethod("getDataAttr");
            getFieldTypeMethod = teamEnumClass.getMethod("getFieldType");
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        Enum[] enumConstants = (Enum[]) teamEnumClass.getEnumConstants();
        for (Enum enumConstant : enumConstants) {
            String dbAttr = null;
            String dataAttr = null;
            Integer fieldType = null;
            try {
                dbAttr = (String) getDbAttrMethod.invoke(enumConstant);
                dataAttr = (String) getDataAttrMethod.invoke(enumConstant);
                fieldType = (Integer) getFieldTypeMethod.invoke(enumConstant);

                Object value = data.get(dataAttr);
                if (Objects.nonNull(value)) {
                    Method getMethod = userTeamClass.getMethod("get" + dbAttr.substring(0, 1).toUpperCase() + dbAttr.substring(1));
                    Method setMethod = userTeamClass.getMethod("set" + dbAttr.substring(0, 1).toUpperCase() + dbAttr.substring(1), getMethod.getReturnType());
                    FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getFieldTypeEnum(fieldType);
                    setMethod.invoke(userTeamEntity, value);
                }

            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }

        return userTeamEntity;

    }




    public static void main(String[] args) {
        CustomerUserEntity customerUserEntity = new CustomerUserEntity();
        customerUserEntity.setDistributionTime(1651L);
        customerUserEntity.setDataId(2L);
        customerUserEntity.setUserName("sdf");
        PaasFormDataEntity paasFormDataEntity = transferTeamEntityToPaasFormDataEntity(customerUserEntity, CustomerTeamEnum.class);
        System.out.println(JSON.toJSONString(paasFormDataEntity));

        UserTeamEntity userTeamEntity = transferPaasFormDataEntityToTeamEntity(paasFormDataEntity, CustomerTeamEnum.class, CustomerUserEntity.class);
        System.out.println(JSON.toJSONString(userTeamEntity));

    }
}
